package com.project.service.impl;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.ILogMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * 用户业务实现类
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private ILogMapper logMapper;

    @Override
    public UserBean login(String code, String pwd) {
        UserBean user = userMapper.login(code,pwd);
        if (user != null){
            LogBean log = new LogBean("用户"+user.getName()+ LocalTime.now().toString()+"登陆系统");
            logMapper.add(log);
        }
        return user;
    }

    @Override
    public boolean add(UserBean user) {
        List<UserBean> userBeanList = userMapper.findByCode(user.getCode());
        if (userBeanList.size()==0){
            userMapper.add(user);
            return true;
        }
        return false;
    }

    @Override
    public void del(int id) {
        userMapper.del(id);
    }

    @Override
    public void update(int id, String grade) {
        UserBean userBean = userMapper.findById(id);
        userMapper.update(userBean.getId(),grade);
    }

    @Override
    public void updatePwd(int id, String pwd) {
        userMapper.updatePwd(id,pwd);
    }

    @Override
    public List<UserBean> findByGrade(String grade) {
        return userMapper.findByGrade(grade);
    }

}
